<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {

            margin: 100px auto 0;
            width: 400px;
        }

        ul {
            list-style: none;
        }

        li {
            margin-top: 10px;
        }

        li>input {
            width: 220px;
        }

        a {
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }

        input {
            height: 26px;
            padding-left: 4px;
            line-height: 26px;
            outline: none;
            border: 0;
            border: 1px solid #ccc;
        }

        #btn {
            margin-top: 10px;
            color: #ff6700;
            cursor: pointer;
        }

        #input {
            margin-top: 10px;
        }

        .show {
            display: block;
        }

        .hide {
            display: none;
        }
    </style>
</head>

<body>
    <div class="box">
        <form action="#" method="post">
            <ul>
                <li>
                    <input type="text" value="1">
                    <a href="javaScript:;" id="pre">[上移]</a>
                    <a href="javaScript:;" id="next">[下移]</a>
                    <a href="javaScript:;" id="delete">[删除]</a>
                </li>
            </ul>
        </form>
        <div id="btn">+ 添加项目</div>
        <div id="input">
            <span>添加到列表：</span>
            <input type="text" id="entry">
            <button id="add">添加</button>
            <button id="cancel">取消</button>
        </div>

    </div>

    <script>

        //单击显示
        var btn = document.getElementById('btn');
        var input = document.getElementById('input');
        var toggle = document.createAttribute('class');
        toggle.nodeValue = 'hide';
        input.setAttributeNode(toggle);
        var flag = 1;
        btn.addEventListener('click', function () {
            if (flag == 0) {
                toggle.nodeValue = 'hide';
                flag++;
            } else {
                toggle.nodeValue = 'show';
                flag--;
            }
            input.setAttributeNode(toggle);
        })

        //添加项目
        var ul = document.querySelector('ul');
        var add = document.getElementById('add');
        var entry = document.getElementById('entry');
        add.addEventListener('click', function () {
            if (entry.value == '') {
                alert('您没有输入内容');
                return false;
            } else {
                var li = document.createElement('li');
                li.innerHTML = '<input type="text" value="' + entry.value + '"> <a href="javaScript:;" id="pre">[上移]</a> <a href="javaScript:;" id="next">[下移]</a> <a href="javaScript:;" id="delete">[删除]</a>';
                ul.appendChild(li);

                //删除项目
                var del = document.querySelectorAll('#delete');
                for (var i = 0; i < del.length; i++) {
                    del[i].onclick = function () {
                        ul.removeChild(this.parentNode);
                    }
                }

                //上移项目
                var pre = document.querySelectorAll('#pre');
                for (var i = 0; i < pre.length; i++) {
                    pre[i].onclick = function () {
                        if (this.parentNode.previousElementSibling == null) {
                            return alert('已移动到最顶端');
                        }
                        ul.insertBefore(this.parentNode, this.parentNode.previousElementSibling);
                    }
                }

                //下移项目
                var next = document.querySelectorAll('#next');
                for (var i = 0; i < next.length; i++) {
                    next[i].onclick = function () {
                        if (this.parentNode.nextElementSibling == null) {
                            return alert('已移动到最低端');
                        }
                        ul.insertBefore(this.parentNode.nextElementSibling, this.parentNode);
                    }
                }
            }
            entry.value = '';
        })

        //取消项目
        var cancel = document.getElementById('cancel');
        cancel.onclick = function () {
            entry.value = '';
        }



    </script>

</body>

</html>